﻿using System;
using Hibiku.IO;
using Hibiku.Models;

namespace Hibiku.Services
{
    public class ApplicationConfigurationService : IApplicationConfigurationService
    {
        private readonly EntityFileReaderWriter<ApplicationConfigurationModel> m_readerWriter;

        public ApplicationConfigurationService()
        {
            m_readerWriter = new EntityFileReaderWriter<ApplicationConfigurationModel>();

            var configuration = m_readerWriter.Read();

            Configuration = (configuration == null ? new ApplicationConfigurationModel() : configuration);
        }

        #region IApplicationConfigurationService Members

        public ApplicationConfigurationModel Configuration
        {
            get;
            private set;
        }

        public void Save()
        {
            m_readerWriter.Write(Configuration);

            var listeners = ConfigurationSaved;

            if (listeners != null)
            {
                listeners(this, EventArgs.Empty);
            }
        }

        public event EventHandler ConfigurationSaved;

        #endregion
    }
}
